package com.caiyouxi.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * sdk工具类
 *
 * @author weichangyong
 * @create 2017-10-21 11:44
 **/
public class NumUtil
{
    /**
     * 生成SDK用户名。以渠道后缀结束
     * 用户名。需要保证所有 记录中唯一。规则为：
     时间戳+4位随机数字+渠道后缀，
     如：14250938305733574.vivo
     * @param user_name_suffix
     * @return
     */
    public static String generate_sdk_app_username(String user_name_suffix)
    {
        StringBuffer username = new StringBuffer();
        username.append(System.currentTimeMillis()).append(4).append(user_name_suffix);
        return username.toString();

    }


    /**
     * 生成指定位数的随机数
     * @param nums
     * @return
     */
    public static String genrate_random_data(int nums)
    {
        StringBuffer sb = new StringBuffer();
        String str = "0123456789";
        Random r = new Random();
        for(int i=0;i<nums;i++){
            int num = r.nextInt(str.length());
            sb.append(str.charAt(num));
            str = str.replace((str.charAt(num)+""), "");
        }
        return sb.toString();
    }


    /**
     * 生成自增ID
     */
    public static String generate_number(int num){
        SimpleDateFormat sdf =   new SimpleDateFormat( "yyMMddHHmmss" );
        String str = sdf.format(new Date()) + genrate_random_data(num);
        return str;
    }

    /**
     * 生成自增ID
     * 自定义位数，最少6位
     */
    public static String generate_number_on_int(int num){
        SimpleDateFormat sdf =   new SimpleDateFormat( "HHmmss" );
        String str = sdf.format(new Date()) + genrate_random_data(num);
        return str;
    }

    /**
     * 判断是否为url
     */
    public static boolean isUrl(String urlPath){
        String regEx = "(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";
        Pattern p = Pattern.compile(regEx);
        Matcher matcher = p.matcher(urlPath);
        return matcher.matches();
    }
}
